package web;

import domain.shop.Product;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.customer.ShopCustomerService;
import web.commons.EshopProductLink;

import java.util.List;

public final class SearchProductPanel extends Panel {

    private final static int RESULTS_ON_PAGE = 10;  //strankujeme po 10
    @SpringBean
    private ShopCustomerService customerService;
    private PageParameters params;
    private IModel<List<Product>> products =
            new LoadableDetachableModel<List<Product>>() {

                @Override
                protected List<Product> load() {
                    return customerService.searchProducts(params.getString("0"));
                }
            };

    public SearchProductPanel(String id) {
        super(id);
        WebMarkupContainer container = new WebMarkupContainer("productContainer");
        add(container);
        container.setVisible(false);

        add(new EmptyPanel("productPaging"));
    }

    public SearchProductPanel(String id, PageParameters params) {
        super(id);
        this.params = params;


        PageableListView<Product> listView = new PageableListView<Product>("productList", products, RESULTS_ON_PAGE) {

            @Override
            protected void populateItem(ListItem<Product> item) {

                final IModel itemModel = item.getModel();

                //do radku zobrazime popisky atributu firmy
                //odkaz na detail
                if (itemModel.getObject() instanceof Product) {

                    String content =
                            getLimitedString(((Product) item.getModelObject()).getShortContent());

                    item.add(new Label("productContent", content).setEscapeModelStrings(false));
                    item.add(new EshopProductLink("productLink", item.getModelObject()));
                }

            }

            private String getLimitedString(String string) {
                if (string.length() > 200) {
                    string = string.substring(0, 200);

                }
                return string;
            }
        };
        //pridame strankovani
        //musime v HTML oznacit cast, ktera se ma pres ajax aktualizovat (cela tabulka)
        WebMarkupContainer container = new WebMarkupContainer("productContainer");

        container.setOutputMarkupId(true);

        add(container);

        container.add(listView); //do kontejneru pridame seznam

        add(new AjaxPagingNavigator("productPaging", listView));
    }
}
